<?php

namespace app\api\controller\mox;

use app\common\controller\Api;
use app\admin\model\mox\inheritor\Info as infoModel;
use app\admin\model\mox\inheritor\Label as LabelModel;

/**
 * 消息提示
 */
class Inheritor extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取故事标签
     */
    public function type()
    {
        $this->success('获取成功', LabelModel::select());
    }
    /**
     * 人物列表
     *
     */
    public function list()
    {
        $title = $this->request->param('title');
        $label_id = $this->request->param('type_id');
        $model = new InfoModel();
        $where = [];
        if ($label_id != 0) {
            $model->whereRaw("FIND_IN_SET(?, label_id)", [$label_id]);
        }
        // 2. 按标题模糊查询
        if ($title != null) {
            $model->whereRaw("title LIKE ?", ["%{$title}%"]);
        }
        $list = $model->order('id desc')
            ->where($where)
            ->field('id, title, image,label_id,brief_introduction,create_time')
            ->paginate()
            ->each(function ($item, $key) {
                //获取标签
                $label_id = explode(',', $item['label_id']);
                $item['label'] = model('app\admin\model\mox\inheritor\Label')->where('id', 'in', $label_id)->field('id, title,color')->select();
            });

        $this->success('请求成功', $list);
    }
    /**
     * 人物详情
     */
    public function inheritorDetail()
    {
        $id = $this->request->param('id');
        if (!$id) {
            $this->error('缺少参数id');
        }
        $model = new infoModel();
        $info = $model->where('id', $id)->find();
        if (!$info) {
            $this->error('人物不存在');
        }
        $info['user'] = model('app\common\model\User')->where('id', $info['user_id'])->field('id, nickname, avatar')->find();
        $label_id = explode(',', $info['label_id']);
        $info['label'] = model('app\admin\model\mox\inheritor\Label')->where('id', 'in', $label_id)->select();
        $related_plays = explode(',', $info['related_plays']);
        $info['related_plays'] = model('app\admin\model\mox\play\Info')->where('id', 'in', $related_plays)->field('id, title, image,type_id,brief_introduction,create_time,update_time')->select();
        foreach ($info['related_plays'] as $k => $v) {
            $v->type = model('app\admin\model\mox\play\Type')->where('id', $v['type_id'])->field('id, title,image')->find();
        }
        $info['plays_num'] = count($related_plays);
        $this->success('请求成功', $info);
    }
}
